티스토리 뷰

공부합시다/php

Laravel Exception Test

IamMH 2023. 11. 15. 16:55

php unit 테스트시 예외처리에 대한 테스트 확인 방법입니다.

<?php

Class SomeTest extends Tests
{
    /**
     * @test
     * @expectedException SomeException
     * @return void
     */
     function some_test(): void
     {
         $this->exceptException(SomeException::class);

        // and some_actions... 
     }
}


Time: 00:00.437, Memory: 6.00 MB

OK (1 test, 1 assertion)

#TL;DR

phpUnit 9.5

$this->expectException(string $exceptionClassName);
$this->expectExceptionCode(int|string $code);
$this->expectExceptionMessage(string $message);
$this->expectExceptionMessageMatches(string $regularExpression);
$this->expectExceptionObject(\Exception $exceptionObject);

참고:
https://stackoverflow.com/questions/5683592/phpunit-assert-that-an-exception-was-thrown
https://phpunit.de/manual/6.5/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.exceptions
https://backendtea.com/post/phpunit-exception-test/

댓글